package com.risevision.riseplayer.timers;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.risevision.riseplayer.Globals;
import com.risevision.riseplayer.utils.Utils;

public class HeartbeatTimer {

	private static Date lastHearbeat = new Date();
	
	static class OnTimerTask extends TimerTask {

		@Override
		public void run() {
			Date now = new Date();
			if ((lastHearbeat.getTime() + Globals.MAX_HEARTBEAT_GAP_MS) < now.getTime()) {
				Utils.restartViewer();
			}
		}

	}

	private static Timer timer;

	public static void start() {
		timer = new Timer();
		
		timer.schedule(new OnTimerTask(), Globals.HEARTBEAT_TIMER_INTERVAL_MS, Globals.HEARTBEAT_TIMER_INTERVAL_MS);
	}

	public static void recordHeartbeat() {
		lastHearbeat = new Date();		
	}

}

